package com.larly.controller;


import com.larly.common.BaseResponse;
import com.larly.common.IdRequest;
import com.larly.common.ResultUtils;
import com.larly.exception.ErrorCode;
import com.larly.exception.ThrowUtils;
import com.larly.model.domain.InteractionLike;
import com.larly.model.vo.InteractionsLikeVO;
import com.larly.service.InteractionLikeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/interaction-like")
public class InteractionLikeController {

    @Resource
    private InteractionLikeService interactionLikeService;

    /**
     * 根据互动获取点赞列表
     *
     */
    @PostMapping("/list")
    public BaseResponse<InteractionsLikeVO> list(@RequestBody IdRequest idRequest) {
        ThrowUtils.throwIf(idRequest == null, ErrorCode.PARAM_ERROR);
        Integer id = idRequest.getId();
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAM_ERROR);
        List<Integer> userIds = interactionLikeService.lambdaQuery()
                .eq(InteractionLike::getInteractionId, id)
                .select(InteractionLike::getUserId)
                .list()
                .stream()
                .map(InteractionLike::getUserId)
                .collect(Collectors.toList());
        InteractionsLikeVO result = new InteractionsLikeVO();
        result.setUserIds(userIds);
        return ResultUtils.success(result);
    }

    /**
     * 获取互动列表根据用户id用于展示能不能点赞
     */
    @PostMapping("/like")
    public BaseResponse<InteractionsLikeVO> listNote(@RequestBody IdRequest idRequest) {
        ThrowUtils.throwIf(idRequest == null, ErrorCode.PARAM_ERROR);
        Integer id = idRequest.getId();
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAM_ERROR);
        List<Integer> noteIds = interactionLikeService.lambdaQuery()
                .eq(InteractionLike::getUserId, id)
                .select(InteractionLike::getInteractionId)
                .list()
                .stream()
                .map(InteractionLike::getInteractionId)
                .collect(Collectors.toList());
        InteractionsLikeVO result = new InteractionsLikeVO();
        result.setInteractionIds(noteIds);
        return ResultUtils.success(result);
    }
}
